package handler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import bean.Group;
import dao.ContactDAO;
import dao.GroupDAO;
import dao.impl.ContactDAOImpl;
import dao.impl.GroupDAOImpl;
import jdbc.ContactsConnection;
import tool.ObjectConverter;

public class GroupHandler extends HttpServlet {
    private static final long serialVersionUID = 4498018100906090725L;
    private Connection conn;
    private GroupDAO groupDAO;
    private ContactDAO contactDAO;
    private PageContext pageContext;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            HttpSession session = req.getSession();
            int accountId = ObjectConverter.castObjectToInteger(session.getAttribute("account-id"));
            List<Group> groups = groupDAO.select(accountId);
            req.setAttribute("groups", groups);
            // 转发请求，目标url-pattern为groups
            pageContext.forward("groups");
        } catch (SQLException | NamingException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operate = req.getParameter("operate");
        try {
            HttpSession session = req.getSession();
            int accountId = ObjectConverter.castObjectToInteger(session.getAttribute("account-id"));
            if (Objects.nonNull(operate) && operate.length() > 0) {
                int groupId = ObjectConverter.parseStringToInteger(req.getParameter("group-id"));
                String groupName = req.getParameter("group-name");
                switch (operate) {
                case "add-group":
                    if (Objects.nonNull(groupName) && groupName.length() > 0) {
                        groupDAO.insert(new Group(groupName, accountId));
                    }
                    resp.sendRedirect("group-handler");
                    break;
                case "edit-group":
                    if (groupId > 0) {
                        Group group = groupDAO.selectById(new Group(groupId, accountId));
                        if (Objects.nonNull(group)) {
                            req.setAttribute("group", group);
                            // 转发请求，目标url-pattern为edit-group
                            pageContext.forward("edit-group");
                        }
                    } else {
                        resp.sendRedirect("group-handler");
                    }
                    break;
                case "modify-group":
                    if (groupId > 0 && Objects.nonNull(groupName) && groupName.length() > 0) {
                        groupDAO.updateById(new Group(groupId, groupName, accountId));
                    }
                    resp.sendRedirect("group-handler");
                    break;
                case "remove-group":
                    if (groupId > 0) {
                        groupDAO.deleteById(contactDAO, new Group(groupId, accountId));
                    }
                    resp.sendRedirect("group-handler");
                    break;
                default:
                    resp.sendRedirect("group-handler");
                }
            }
        } catch (SQLException | NamingException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        pageContext = JspFactory.getDefaultFactory().getPageContext(this, req, resp, getServletInfo(), true, 0, true);
        super.service(req, resp);
    }

    @Override
    public void destroy() {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        try {
            conn = ContactsConnection.open();
            groupDAO = new GroupDAOImpl(conn);
            contactDAO = new ContactDAOImpl(conn);
        } catch (NamingException | SQLException e) {
            e.printStackTrace();
        }
        super.init();
    }
}
